import InfoBarList from '../InfoBarList/InfoBarList';

const backgroundId = 'e9424af07d8242adb4fa49abe40544b4';
const topId = '05ad118feea64a349fb2e9fd3eba730b';

export default class TemperatureHandler {
    constructor() {
        this.chart = null;
        this.infoObj = null;
        this.legendObj = {
            name: '温度',
            arrName: ['<10', '10-20', '20-30', '>30'],
            arrColor: ['rgb(230, 245, 190)', 'rgb(254, 224, 139)', 'rgb(244, 108, 67)', 'rgb(213, 62, 80)']
        };
        this.num = 0;
        this.visible = false;
    }

    loadData(map, isInitChart) {
        let that = this;
        minemap.util.getJSON('../data/tem.json', function (error, data) {
            const dataArr = data ? data.data.rows[0].features || [] : [];
            that.infoObj = {
                "color1": "#00C0CE",
                "color2": "#FEC819",
                "unit": "温度（℃）",
                "array": [],
                // callback: (coordinate) => {
                //     map.flyTo({
                //         center: coordinate,
                //         zoom: 15
                //     });
                // }
            };
            dataArr.map((item) => {
                that.infoObj.array.push({
                    "name": item.properties.name_zh,
                    "value": item.properties.tem,
                    "coordinate": item.geometry.coordinates[Math.ceil(item.geometry.coordinates.length / 2)]
                })
            });
            if (isInitChart) {
                that.updateChart();
            }
        });
    }

    updateChart() {
        this.chart = new InfoBarList('road-weather-chart', this.infoObj || {});
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }

    getLegendObj() {
        return this.legendObj;
    }

    setValueNum(map, valueNum) {
        if (this.visible) {
            map.moveLayer('tem' + this.num, backgroundId);
            this.num = valueNum;
            map.moveLayer('tem' + valueNum, topId);
        } else {
            this.num = valueNum;
        }
    }

    setVisible(map) {
        if(!this.visible) {
            for (let i = 0; i < 25; i++) {
                map.setLayoutProperty('tem' + i, 'visibility', 'visible');
            }
            map.moveLayer('tem' + this.num, topId);
            this.visible = true;
        }
    }

    setUnvisible(map) {
        if (this.visible) {
            for (let i = 0; i < 25; i++) {
                map.setLayoutProperty('tem' + i, 'visibility', 'none');
            }
            map.moveLayer('tem' + this.num, backgroundId);
            this.visible = false;
        }
    }
};